<template>
  <div class="dashboard-container">
    <div class="permission-container">
      <div class="app-container">
        <h2>权限管理</h2>
        <!-- 表格 -->
        <el-card>
          <div style="text-align: right; margin-bottom: 20px">
            <el-button type="primary" size="small" @click="addPermissionfn(1, '0')">添加权限</el-button>
          </div>
          <!-- row-key指定对象里属性(不重复唯一的, 作为每个节点的标识类似v-for中key) -->
          <el-table border :data="permissionList" row-key="id">
            <el-table-column label="名称" prop="name" />
            <el-table-column label="标识" prop="code" />
            <el-table-column label="描述" prop="description" />
            <el-table-column label="操作">
              <template v-slot="scope">
                <el-button v-if="scope.row.pid === '0'" type="text" @click="addPermissionfn(2, scope.row.id)">添加</el-button>
                <el-button type="text" @click="editFn(scope.row.id)">编辑</el-button>
                <el-button type="text" @click="delFn(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>

        <!-- 新增-权限点-弹框 -->
        <per-dialog
          ref="perDialog"
          :show-dialog.sync="showPerDialog"
          :click-id="clickId"
          :is-edit="isEdit"
          :origin-list="perList"
          @confirmEV="confirmFn"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { getPermissionListAPI, addPermissionAPI, getPermissionDetailAPI, updatePermissionAPI, delPermissionAPI } from '@/api'
import { transTree } from '@/utils'
import perDialog from './components/perDialog.vue'
export default {
  components: {
    perDialog
  },
  data() {
    return {
      permissionList: [], // 权限点列表(树形结构)
      perList: [], // 权限点列表(扁平结构)
      showPerDialog: false, // 控制新增-权限点-弹窗是否出现
      type: 0, // 添加页面权限/按钮权限(1/2)
      pid: '', // 新增权限点对象的pid值使用
      isEdit: false, // 同一个确定按钮前提下, 区分是新增/编辑(false/true)
      clickId: '' // 点击那项(行)数据对象的id
    }
  },
  created() {
    // 请求权限列表数据
    this.getPermissionListFn()
  },
  methods: {
    // 获取-权限列表数据
    async getPermissionListFn() {
      const res = await getPermissionListAPI()
      // console.log(res)
      this.perList = res.data
      this.permissionList = transTree(res.data, '0')
    },
    // 添加权限-按钮点击事件-方法
    addPermissionfn(type, pid) {
      this.isEdit = false
      this.showPerDialog = true
      // 保存到组件的属性变量上，为了在别的函数使用这两个值
      this.type = type
      this.pid = pid
      this.clickId = pid
    },
    // 添加权限->接口调用(弹框点确定了)
    async confirmFn(dataObj) {
      if (this.isEdit) {
        // 编辑状态->dataObj里已经有了type和pid了
        const res = await updatePermissionAPI(dataObj)
        console.log(res)
      } else {
        /**
       * {
       * ...dataObj
       * type: this.type,
       * pid = this.pid
       * }
       */
        dataObj.type = this.type
        dataObj.pid = this.pid
        const res = await addPermissionAPI(dataObj)
        console.log(res)
      }
      // 请求权限列表数据
      this.getPermissionListFn()
    },
    // 编辑权限点->点击事件
    async editFn(id) {
      this.isEdit = true
      this.showPerDialog = true
      // 请求详细信息
      const res = await getPermissionDetailAPI(id)
      this.$refs.perDialog.formData = res.data
    },
    // 删除权限->点击事件
    async delFn(id) {
      await delPermissionAPI(id)
      this.getPermissionListFn()
    }
  }
}
</script>

<style lang="scss" scoped></style>
